package com.example.restful.Controller;
import com.example.restful.domain.User;
import com.example.restful.common.ResponseData;
import com.example.restful.mapper.UserDao;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "111")
@RestController
@RequestMapping("/users")
public class UserController {
    @GetMapping("/{id}")
    @Operation(summary = "随机获取一个用户")
    public ResponseData getUser(@PathVariable int id){
        User user = UserDao.findUserById(id);
        return new ResponseData("200",null,true,user);
    }
    @GetMapping("")
    public ResponseData getAllUsers(){
        Map<Integer,User> users=UserDao.findAllUsers();
        return new ResponseData("200",null,true,users);
    }
    @PostMapping("")
    public ResponseData addUser(@RequestBody User user){
        UserDao.addUser(user);
        return new ResponseData<>("200",null,true,null);
    }
    @PutMapping("")
    public ResponseData updateUser(@RequestBody User user){
        UserDao.updateUser(user);
        return new ResponseData("200",null,true,null);
    }
    @DeleteMapping("/{id}")
    public ResponseData deleteUser(@PathVariable int id){
        UserDao.deleteUser(id);
        return new ResponseData("200",null,true,null);
    }
}
